﻿namespace ImageProcessing.Forms
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Domains;
    using MathWorks.MATLAB.NET.Arrays;
    using MathWorks.MATLAB.NET.Utility;
    using MATLABApp;
    using Test2;
    using Untitled1;

    /// <summary>
    /// Frequency domain form
    /// </summary>
    public partial class FrequencyDomain : Form
    {
        /// <summary>
        /// The fourier image
        /// </summary>
        private FourierImage fourierImage;

        /// <summary>
        /// The width and height
        /// </summary>
        private int u, v;

        /// <summary>
        /// Refresh boolean
        /// </summary>
        private bool refresh = false;

        /// <summary>
        /// Initializes a new instance of the <see cref="FrequencyDomain"/> class.
        /// </summary>
        /// <param name="img">The img.</param>
        public FrequencyDomain(DImage img)
        {
            this.InitializeComponent();
            this.fourierImage = new FourierImage(img);

            foreach (DImage imgage in Main.ImageList)
            {
                this.CmbImage.Items.Add(imgage.Name);
            }

            this.CmbImage.SelectedIndex = 0;
            this.PcbImage.Image = Operations.DisplayMatrix(Main.ImageList[this.CmbImage.SelectedIndex].ImageStructArray);
        }

        /// <summary>
        /// Runs the result.
        /// </summary>
        private void RunResult()
        {
            this.PcbResult.Image = Operations.DisplayMatrix(this.fourierImage.InitAndReturn());
            this.DisplayLogMag();
        }

        /// <summary>
        /// Displays the log mag.
        /// </summary>
        private void DisplayLogMag()
        {
            ImageStructure[,] redpicbox = new ImageStructure[this.u, this.v];
            ImageStructure[,] greenpicbox = new ImageStructure[this.u, this.v];
            ImageStructure[,] bluepicbox = new ImageStructure[this.u, this.v];

            this.pictureBox1.Image = Operations.DisplayMatrix(PixelOperation.NormalizationOneColor(this.fourierImage.RedLogMagnitude, this.u, this.v, 'r'));
            this.pictureBox2.Image = Operations.DisplayMatrix(PixelOperation.NormalizationOneColor(this.fourierImage.GreenLogMagnitude, this.u, this.v, 'g'));
            this.pictureBox3.Image = Operations.DisplayMatrix(PixelOperation.NormalizationOneColor(this.fourierImage.BlueLogMagnitude, this.u, this.v, 'b'));
            this.GrayImage.Image = this.pictureBox4.Image = Operations.DisplayMatrix(PixelOperation.NormalizationOneColor(this.fourierImage.GrayLogMagnitude, this.u, this.v, 'f'));
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the CmbImage control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void CmbImage_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.fourierImage.Image = Main.ImageList[this.CmbImage.SelectedIndex];
            this.u = this.fourierImage.Image.Height; 
            this.v = this.fourierImage.Image.Width;
            this.LblH.Text = " H: " + this.u.ToString(); 
            this.LblW.Text = " W: " + this.v.ToString();
            this.LblName.Text = " Name: " + this.fourierImage.Image.Name;
            this.PcbImage.Image = BestBicubic.PictureResize((Image)Main.ImageList[this.CmbImage.SelectedIndex].ImageBitmap, new Size(this.PcbImage.Width, this.PcbImage.Height));
            this.RunResult();
        }

        /// <summary>
        /// Handles the Click event of the BtnCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.fourierImage.Image.RetriveImageArray();
            this.Close();
        }

        /// <summary>
        /// Handles the Enter event of the groupBox4 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void GroupBox4_Enter(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// Handles the MouseMove event of the GrayImage control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
        private void GrayImage_MouseMove(object sender, MouseEventArgs e)
        {
            PictureBox pictureBox = (PictureBox)sender;

            this.LblX.Text = " X: " + e.X;
            this.LblY.Text = " Y: " + e.Y;
            this.LblFourrier.Text = " Fourrier: Gray Fourrier";
        }

        /// <summary>
        /// Handles the MouseMove event of the PictureBox1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
        private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            this.LblX.Text = " X: " + e.X;
            this.LblY.Text = " Y: " + e.Y;
            this.LblFourrier.Text = " Fourrier: Blue Fourrier";
        }

        /// <summary>
        /// Handles the MouseMove event of the PictureBox2 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
        private void PictureBox2_MouseMove(object sender, MouseEventArgs e)
        {
            this.LblX.Text = " X: " + e.X;
            this.LblY.Text = " Y: " + e.Y;
            this.LblFourrier.Text = " Fourrier: Green Fourrier";
        }

        /// <summary>
        /// Handles the MouseMove event of the PictureBox3 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
        private void PictureBox3_MouseMove(object sender, MouseEventArgs e)
        {
            this.LblX.Text = " X: " + e.X;
            this.LblY.Text = " Y: " + e.Y;
            this.LblFourrier.Text = " Fourrier: Red Fourrier";
        }

        /// <summary>
        /// Handles the MouseLeave event of the PictureBox3 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void PictureBox3_MouseLeave(object sender, EventArgs e)
        {
            this.LblX.Text = " X: ";
            this.LblY.Text = " Y: ";
            this.LblFourrier.Text = " Fourrier:";
        }

        /// <summary>
        /// Handles the Click event of the BtnFEffects control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnFEffects_Click(object sender, EventArgs e)
        {
            FourierEffectsForm fourierEffects = new FourierEffectsForm(this.fourierImage);
            fourierEffects.Show();
            this.refresh = true;
        }

        /// <summary>
        /// Handles the Click event of the BtnOk control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnOk_Click(object sender, EventArgs e)
        {
            this.fourierImage.Image.UpdateImage();
            this.Close();
        }

        /// <summary>
        /// Handles the MouseEnter event of the FrequencyDomain control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void FrequencyDomain_MouseEnter(object sender, EventArgs e)
        {
            if (this.refresh)
            {
                this.PcbResult.Image = Operations.DisplayMatrix(this.fourierImage.Image.ImageStructArray);
                this.fourierImage.Init();
                this.pictureBox4.Image = Operations.DisplayMatrix(PixelOperation.NormalizationOneColor(this.fourierImage.GrayLogMagnitude, this.u, this.v, 'f'));
                this.refresh = false;
            }
        }
    }
}
